package com.trigsoft.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

import android.content.Context;

public class FileUtils
{
	public static void write(String name, String text)
	{
		try
		{
			PrintWriter out = new PrintWriter(new File(name).getAbsoluteFile());
			try
			{
				out.print(text);
			} finally
			{
				out.close();
			}
		} catch (IOException e)
		{
			throw new RuntimeException(e);
		}
	}

	public static void write(Context context, String name, String text)
	{
		try
		{
			PrintWriter out = new PrintWriter(context.openFileOutput(name, Context.MODE_PRIVATE));
			try
			{
				out.print(text);
			} finally
			{
				out.close();
			}
		} catch (IOException e)
		{
			throw new RuntimeException(e);
		}
	}

	public static String read(Context context, String name)
	{
		StringBuilder sb = new StringBuilder();
		try
		{
			BufferedReader in = new BufferedReader(new InputStreamReader(context.openFileInput(name)));
			try
			{
				String s;
				while ((s = in.readLine()) != null)
				{
					sb.append(s);
					sb.append("\n");
				}
			} finally
			{
				in.close();
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static ArrayList<String> getLines(Context context, String name)
	{
		// TODO Auto-generated method stub
		ArrayList<String> as = new ArrayList<String>();
		try
		{
			BufferedReader in = new BufferedReader(new InputStreamReader(context.openFileInput(name)));
			try
			{
				String s;
				while ((s = in.readLine()) != null)
				{
					as.add(s);
				}
			} finally
			{
				in.close();
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return as;
	}
}
